<?php

include "../../lib/start.php";

check_session();
check_permission(ADM_PERM);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dettaglio alunno</title>
<link rel="stylesheet" href="../../css/main.css" type="text/css" />
<script type="text/javascript" src="../../js/prototype.js"></script>
<script type="text/javascript" src="../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../js/page.js"></script>
<script type="text/javascript"></script>
</head>
<body>
<form action="send_file.php?action=upload" method="post" enctype="multipart/form-data" id="doc_form">
<div style="height: 25px; display: inline" id="_div">
<?php if(!isset($_REQUEST['action'])){ ?>
<input class="form_input" type="file" name="fname" class="file" style="width: 427px" onchange="parent.loading(30); document.forms[0].submit()" />
<?php 
}
else{
?>
<span style="font-weight: bold; color: red" id="_span">Attendere la copia del file...</span>
<?php }
flush();
flush();
?>
</div>
</form>
</body>
</html>
<?php
// create
flush();
flush();
if($_REQUEST['action'] == "upload"){
	/**
	* gestione del filesystem
	*/
	$file_name = basename( $_FILES['fname']['name']);
	$file = ereg_replace(" ", "_", basename( $_FILES['fname']['name']));
	
	/**
	 * gestione file nel filesystem
	 */
	$dir = $_SESSION['__config__']['document_root']."download";
	$link = $file;
	
	$target_path = $dir . "/" . $file;
	//print $target_path;
	print("<script>$('_span').innerHTML = 'Attendere il caricamento del file...'</script>");
	if(move_uploaded_file($_FILES['fname']['tmp_name'], $target_path)) {
		//echo "The file ".  basename( $_FILES['fname']['name']). " has been uploaded";
		chdir($dir);
		chmod($file, 0644);
	} else{
		//fwrite($log, "There was an error uploading the file, please try again!\n");
		echo "ko|There was an error uploading the file, please try again!|".$_FILES['fname']['name'];
		//fclose($log);
		exit;
	}
	print("<script>parent.timeout = 0; $('_span').innerHTML = '$file_name'; $('_span').style.color = 'black'; parent.document.forms[0].server_file.value = '$file';</script>");
}

?>